package com.yyplatform.admin.platform.config.controller;

import static com.yyplatform.admin.Constants.URL_PREFIX;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yyplatform.admin.infra.model.RestResponse;
import com.yyplatform.admin.platform.config.model.dto.SysDictDTO;
import com.yyplatform.admin.platform.config.model.dto.SysDictFormDTO;
import com.yyplatform.admin.platform.config.model.dto.SysDictQueryDTO;
import com.yyplatform.admin.platform.config.service.SysDictService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * Created on 2024-09-13
 */
@Slf4j
@Tag(name = "配置/字典管理")
@AllArgsConstructor
@RestController
@RequestMapping(URL_PREFIX + "/sys/dict/")
public class SysDictController {

    private SysDictService sysDictService;


    @GetMapping("query")
    @Operation(summary = "查询")
    @RequiresPermissions("sys:dict:query")
    public RestResponse<Page<SysDictDTO>> query(SysDictQueryDTO queryDTO) {
        log.debug("query dto = {}", queryDTO);

        Page<SysDictDTO> page = sysDictService.query(queryDTO);

        return RestResponse.ok(page);
    }


    @GetMapping("get")
    @Operation(summary = "通过ID获取详情")
    @RequiresPermissions("sys:dict:get")
    public RestResponse<SysDictDTO> get(Long id) {
        log.debug("get by id is {}", id);

        SysDictDTO data = sysDictService.get(id);
        return RestResponse.ok(data);
    }


    @PostMapping("save")
    @Operation(summary = "保存")
    @RequiresPermissions("sys:dict:save")
    public RestResponse<Void> save(@RequestBody SysDictFormDTO sysDictFormDTO) {
        log.debug("save dto is {}", sysDictFormDTO);

        sysDictService.save(sysDictFormDTO);

        return RestResponse.ok(null);
    }


    @PostMapping("update")
    @Operation(summary = "修改")
    @RequiresPermissions("sys:dict:update")
    public RestResponse<Void> update(@RequestBody SysDictFormDTO formDTO) {
        log.debug("update dto is {}", formDTO);

        sysDictService.update(formDTO);
        return RestResponse.ok(null);
    }


    @PostMapping("delete")
    @Operation(summary = "删除")
    @RequiresPermissions("sys:dict:delete")
    public RestResponse<Void> delete(Long id) {
        log.debug("delete by id is {}", id);

        sysDictService.delete(id);
        return RestResponse.ok(null);
    }


    @PostMapping("batchDelete")
    @Operation(summary = "批量删除")
    @RequiresPermissions("sys:dict:batchDelete")
    public RestResponse<Void> batchDelete(String ids) {
        log.debug("delete by ids is {}", ids);

        Set<Long> idSet = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toSet());

        sysDictService.delete(idSet);
        return RestResponse.ok(null);
    }

}
